package com.app.piemoment;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import com.app.piemoment.entity.City;
import com.app.piemoment.entity.CityListener;
import com.app.piemoment.entity.CitySelectDialog;
import com.app.piemoment.entity.PieUser;

public class ComplateUserBaseActivity extends Activity {
	public int sex = 1;
	EditText nickName_EditText, familyname_EditText, givenname_EditText,
			sex_EditText, birthday_EditText;
	SharedPreferences userInfo;
	Button ok_Button, hometown_EditText;

	private PieMomentApp ta = null;

	private class InitializeThread implements Runnable {

		public void run() {

			List<String> provincesList = new ArrayList<String>();
			Map<String, List> citiesMap = new HashMap<String, List>();
			Map<String, List> districtsMap = new HashMap<String, List>();
			Resources res = getResources();
			AssetManager am = getAssets();
			InputStream cis = null;
			InputStream dis = null;
			BufferedReader reader = null;
			try {
				cis = am.open(res.getString(R.string.cities_txt_url));
				dis = am.open(res.getString(R.string.districts_txt_url));
				reader = new BufferedReader(new InputStreamReader(cis, res
						.getString(R.string.charset)));
				String contentLine = null;
				while ((contentLine = reader.readLine()) != null) {
					String[] items = contentLine.split(",");
					int lenItem = items.length;
					provincesList.add(items[0]);

					List citiesList = new ArrayList();
					for (int i = 1; i < lenItem; i++) {
						citiesList.add(items[i]);
					}
					citiesMap.put(items[0], citiesList);
				}

				reader = new BufferedReader(new InputStreamReader(dis, res
						.getString(R.string.charset)));
				while ((contentLine = reader.readLine()) != null) {
					String[] items = contentLine.split(",");
					List districtsList = new ArrayList();
					for (int i = 1; i < items.length; i++) {
						districtsList.add(items[i]);
					}
					districtsMap.put(items[0], districtsList);
				}

				ta.setProvincesList(provincesList);
				ta.setCitiesMap(citiesMap);
				ta.setDistrictsMap(districtsMap);

			} catch (Exception e) {
				Log.e("InitializeThread read city", e.toString());
			} finally {
				try {
					cis.close();
					dis.close();
					// am.close();
					if (null != reader) {
						reader.close();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.regedit_info_step1);
		ta = (PieMomentApp) getApplicationContext();
		new Thread(new InitializeThread()).start();
		userInfo = getSharedPreferences(BaseActivity.PREF_NAME,
				MODE_WORLD_READABLE);
		nickName_EditText = (EditText) findViewById(R.id.nickName_EditText);
		familyname_EditText = (EditText) findViewById(R.id.familyname_EditText);
		givenname_EditText = (EditText) findViewById(R.id.givenname_EditText);
		hometown_EditText = (Button) findViewById(R.id.hometown_EditText);
		hometown_EditText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				City startCity = new City();
				CitySelectDialog cityDialog = new CitySelectDialog(
						ComplateUserBaseActivity.this, 0, startCity,
						new CityListener() {

							@Override
							public void onCityResult(City city, int whichOne) {
								hometown_EditText.setText(city.getLastSpace());
							}
						});

			}
		});

		sex_EditText = (EditText) findViewById(R.id.sex_EditText);
		sex_EditText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (sex == 1) {
					sex_EditText.setText("女");
					sex = 0;
				} else {
					sex_EditText.setText("男");
					sex = 1;
				}

			}
		});
		birthday_EditText = (EditText) findViewById(R.id.birthday_EditText);
		birthday_EditText.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					showDataSelect();
				}

			}
		});
		birthday_EditText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDataSelect();

			}
		});

		ok_Button = (Button) findViewById(R.id.ok_button);
		ok_Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				userInfo.edit().putString(PieUser.nickname_S,
						nickName_EditText.getText().toString()).commit();
				userInfo.edit().putString(PieUser.first_name_S,
						familyname_EditText.getText().toString()).commit();
				userInfo.edit().putString(PieUser.surnname_S,
						givenname_EditText.getText().toString()).commit();
				userInfo.edit().putString(PieUser.sex_S, sex + "").commit();
				userInfo.edit().putString(PieUser.birthday_S,
						birthday_EditText.getText().toString()).commit();
				userInfo.edit().putString(PieUser.birth_place_S,
						hometown_EditText.getText().toString()).commit();

				Intent intent = new Intent();
				intent.setClass(ComplateUserBaseActivity.this,
						ComplateUserInfo2Activity.class);
				startActivity(intent);

			}
		});
		sex_EditText.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (sex == 1) {
					sex_EditText.setText("女");
					sex = 0;
				} else {
					sex_EditText.setText("男");
					sex = 1;
				}

			}
		});

	}

	private void showDataSelect() {

		int year = 0;
		int monthOfYear = 0;
		int dayOfMonth = 0;
		if (birthday_EditText != null) {
			try {
				String[] time = birthday_EditText.getText().toString().split(
						"-");
				year = Integer.parseInt(time[0]);
				monthOfYear = Integer.parseInt(time[1]) - 1;
				dayOfMonth = Integer.parseInt(time[2]);
			} catch (Exception e) {
				final Calendar c = Calendar.getInstance();
				year = c.get(Calendar.YEAR);
				// 获取当前年份
				monthOfYear = c.get(Calendar.MONTH);// 获取当前月份
				dayOfMonth = c.get(Calendar.DAY_OF_MONTH);// 获取当前月份的日期号码
			}
		}
		new DatePickerDialog(ComplateUserBaseActivity.this, dataSetListener,
				year, monthOfYear, dayOfMonth).show();

	}

	private OnDateSetListener dataSetListener = new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			String date = pad(year) + "-" + pad(monthOfYear + 1) + "-"
					+ pad(dayOfMonth);
			birthday_EditText.setText(date);
		}
	};

	private String pad(int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}
}
